from redis import Redis, StrictRedis

redis = StrictRedis(host="127.0.0.1", port=6379, db=0)

if __name__ == '__main__':
    # # 设置一个字符串
    # redis.set("name","xiaoming")
    # # 设置有效期的字符串
    # redis.setex("sms:13365786578",300,"30303")
    # redis.setex("sms:13365711178",300,"12345")

    # 删除一个键
    # redis.delete("name")

    # 设置一个hash
    # redis.hset("humen", "name", "xiaoming")
    # redis.hmset("humen", {"age": 17, "sex": 2})

    # 获取一个字符串数据
    print( redis.get("nickname") ) # b'xiaoming'


    # 获取一个列表
    print(redis.lrange("brother",0,-1)) # [b'xiaoF', b'xiaohong', b'xiaohui']

    # 获取一个哈希hash
    humen = redis.hgetall("humen")
    print(humen) # {b'name': b'xiaoming', b'age': b'17', b'sex': b'2'}
    print( humen[b"name"] )
    print( humen["name".encode()] )

    # 获取列表的指定下标的成员
    print(redis.lindex("brother",2)) # b'xiaohui'

    # 获取哈希中指定属性的成员
    print(redis.hget("humen", "name")) # b'xiaoming'

    # 获取数据，不存在，不会报错，而是返回None
    print(redis.get("sms:13365786578"))
    # 获取有效期
    print(redis.ttl("sms:13365786578")) # 不存在，则-2

    # 设置数据有效期
    redis.expire("humen",3000)